package com.xmb.common.service.impl;

import com.tencent.cloud.CosStsClient;
import com.xmb.common.service.CosService;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.TreeMap;

/**
 * Created by lyndon on 2019/4/9.
 */
@Service
public class CosServiceImpl implements CosService{

    public String getCosAuthInfo() throws IOException {

        TreeMap<String, Object> config = new TreeMap<String, Object>();

        // 替换为您的 SecretId
        config.put("SecretId", "AKID6KJciyCsU8YiofQ3D8QTdPwLeEthrURc");
        // 替换为您的 SecretKey
        config.put("SecretKey", "ATBQY1PatVqE6uc6rQnLsLgifpX4Ng6K");

        // 临时密钥有效时长，单位是秒
        config.put("durationSeconds", 1800);

        // 换成您的 bucket
        config.put("bucket", "hh-image-small-1254083899");
        // 换成 bucket 所在地区
        config.put("region", "ap-guangzhou");

        // 这里改成允许的路径前缀，可以根据自己网站的用户登录态判断允许上传的目录，例子：* 或者 doc/* 或者 picture.jpg
        config.put("allowPrefix", "*");

        // 密钥的权限列表。简单上传、表单上传和分片上传需要以下的权限，其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
        String[] allowActions = new String[] {
                // 简单上传
                "name/cos:PutObject",
                // 表单上传、小程序上传
                "name/cos:PostObject",
                // 分片上传
                "name/cos:InitiateMultipartUpload",
                "name/cos:ListMultipartUploads",
                "name/cos:ListParts",
                "name/cos:UploadPart",
                "name/cos:CompleteMultipartUpload"
        };
        config.put("allowActions", allowActions);
        JSONObject credential = CosStsClient.getCredential(config);
        //成功返回临时密钥信息，如下打印密钥信息
        return credential.toString();
    }
}
